<template>
  <div class="login-container">
    <div class="container_form">
      <div class="container_title">
        <img src="~@/assets/static/login/title.png" />
        <p class="p_title">综合管理</p>
        <img class="login_bg3" src="~@/assets/static/login/login_bg3.png" />
      </div>
      <div class="form">
        <div class="form_con">
          <div class="form_img">
            <img src="../../assets/code.png" />
            <p>使用手机浏览器扫码下载APP</p>
          </div>
          <div class="form_btn">
            <el-form ref="loginForm" class="login-form" label-position="left">
              <el-form-item prop="username">
                <span class="svg-container">
                  <svg-icon icon-class="user" />
                </span>
                <el-input
                  ref="username"
                  v-model="username"
                  placeholder="手机号"
                  name="username"
                  type="text"
                  tabindex="1"
                />
              </el-form-item>

              <el-form-item prop="password">
                <span class="svg-container">
                  <svg-icon icon-class="password" />
                </span>
                <el-input
                  :key="passwordType"
                  ref="password"
                  v-model="password"
                  :type="passwordType"
                  placeholder="密码"
                  name="password"
                  tabindex="2"
                />
                <span class="show-pwd" @click="showPwd">
                  <svg-icon
                    :icon-class="
                      passwordType === 'password' ? 'eye' : 'eye-open'
                    "
                  />
                </span>
              </el-form-item>

              <el-button
                :loading="loading"
                style="background: #409eff !important;color: #fff;"
                @click.native.prevent="handleLogin"
                >登录</el-button
              >
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      username: "",
      password: "",
      loading: false,
      passwordType: "password",
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    showPwd() {
      this.passwordType = this.passwordType === "password" ? "" : "password";
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.loading = true;
      this.$store
        .dispatch("admin/login", {
          username: this.username,
          password: this.password,
          platform: 'web'
        })
        .then(() => {
          this.$router.push({ path: this.redirect || "/" });
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
    font-size: 16px !important;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      caret-color: $cursor;
      font-size: 22px;
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container {
  min-height: 100%;
  width: 100%;
  overflow: hidden;
  background-image: url("~@/assets/static/login/login_bg.png");
  background-size: 100% 100%;
  position: relative;
  .container_form {
    width: 46.8%;
    height: 60%;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .container_title {
      width: 100%;
      height: 20%;
      text-align: center;
      margin-bottom: 4%;

      .img_div {
        height: 100%;
      }
      img {
        width: 100%;
        height: 30px;
      }
      .p_title {
        color: #fff;
        font-size: 28px;
      }
      .login_bg3 {
        position: absolute;
        left: 0;
        top: -41px;
        width: 100%;
        height: 24%;
        z-index: -1;
      }
    }
    .form {
      width: 100%;
      min-height: 70%;
      background-image: url("~@/assets/static/login/login_bg2.png");
      background-size: 100% 100%;
      position: relative;

      .form_con {
        width: 80%;
        height: 70%;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        display: flex;
        padding: 5px 10px;
        box-sizing: border-box;
      }
      .form_img {
        flex: 2;
        height: 96%;
        padding: 22px 0;
        background: rgba(26, 29, 72, 0.8);
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        justify-content: center;
        color: #fff;
        font-size: 0.8rem;
        margin-right: 20px;
        img {
          max-width: 78%;
          max-height: 78%;
        }
        p {
          margin: 0;
          padding: 0;
        }
      }
      .form_btn {
        flex: 3;
        padding: 0 20px;
        box-sizing: border-box;
        display: flex;
        align-items: center;
        .login-form {
          width: 100%;
        }
        .svg-container {
          color: #fff;
          margin-left: 15px;
        }
      }
      .el-form-item {
        border: 1px solid #73a5ff;
        border-radius: 0;
        background-image: linear-gradient(to right, #464f9d, #4d8ccc);
      }
      .el-button {
        border: none;
        background-image: linear-gradient(to right, #6571dd, #4699ed);
        font-size: 18px;
        width: 100%;
        margin-top: 8%;
      }
    }
  }
  .show-pwd {
    position: absolute;
    right: 16px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
::v-deep .el-input__inner:hover{
  color: #fff !important;
}
</style>
